//
//  BezierView.m
//  DrawBezierDemo
//
//  Created by wulanzhou on 16/5/12.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BezierView.h"

#define degreesToRadians(degrees) (degrees * M_PI / 180.0)  //角度转弧度
#define circleRadius  ([UIScreen mainScreen].bounds.size.width>320?130.0:100.0) // iphone 6=130 iphone5=100
#define kArcLineWidth 60.0 //线宽


@interface BezierView ()
@property (nonatomic,assign) CGPoint circleCenter; //中心点
@end

@implementation BezierView


- (id)initWithFrame:(CGRect)frame{

    if (self=[super initWithFrame:frame]) {
        
        CGFloat topY=[UIScreen mainScreen].bounds.size.width>320?220.0:180.0;

        //圆的中心点
        self.circleCenter=CGPointMake(self.bounds.size.width/2.0, topY); // iphone 6=220 iphone5=180
        
        //背景色
        self.backgroundColor=[UIColor colorWithRed:24/255.0 green:24/255.0 blue:24/255.0 alpha:1.0];
        
        //三角形箭头
        self.arrowView=[[BezierArrowView alloc] initWithFrame:CGRectMake(20, 100, 24, 24)];
        [self addSubview:self.arrowView];
        
    }
    return self;
}

/**
 *  设置数据源
 *
 *  @param doorAir
 */
- (void)setBezierDoorAir:(BasedoorAir *)doorAir{

    if (_bezierDoorAir!=doorAir) {
        _bezierDoorAir=doorAir;
        [self setNeedsDisplay];
    }
}



/**
 *  重写画图方法
 *
 *  @param rect
 */
- (void)drawRect:(CGRect)rect {
    
    
    //设置扇形背景
    [self drawFannedLineColor:[UIColor colorWithRed:85/255.0 green:85/255.0 blue:85/255.0 alpha:1.0] startAngle:-150.0 endAngle:-30.0];
    
    if (self.bezierDoorAir) {
        
        NSArray *source=[self.bezierDoorAir GetIndexLevelColors];
        
        if (source&&[source count]>0) {
            
            for (BezierdoorAngle *item in source) {
                //画进度
                [self drawFannedLineColor:item.bezierColor startAngle:item.startAngle endAngle:item.endAngle];
            }
            
            BezierdoorAngle *lastModel=[source lastObject];
            //设置指示三角形与文字drawTriangleWithAngle:(endAngle-1)
            [self drawTriangleWithAngle:lastModel.endAngle];
        }
        
    }
    
    /**  以下为测试方法
     
    //设置扇形背景
    [self drawFannedLineColor:[UIColor colorWithRed:85/255.0 green:85/255.0 blue:85/255.0 alpha:1.0] startAngle:-150.0 endAngle:-30.0];
    
    
    //设置第一个进度
    [self drawFannedLineColor:[UIColor colorWithRed:146/255.0 green:251/255.0 blue:162/255.0 alpha:1.0] startAngle:-150.0 endAngle:-138.0];
    //设置第二个进度
    [self drawFannedLineColor:[UIColor colorWithRed:0/255.0 green:250/255.0 blue:57/255.0 alpha:1.0] startAngle:-139.0 endAngle:-128.0];
    
    [self drawFannedLineColor:[UIColor colorWithRed:2/255.0 green:56/255.0 blue:57/255.0 alpha:1.0] startAngle:-129.0 endAngle:-35.0];
    
    //设置指示三角形与文字drawTriangleWithAngle:(endAngle-1)
    [self drawTriangleWithAngle:-129];
     
     **/
    
}


#pragma mark -辅助方法

/**
 *  以任意点为圆心 旋转UIView
 *
 *  @param centerX 原始中心点x
 *  @param centerY 原始中心点y
 *  @param x       新的中心点x
 *  @param y       新的中心点y
 *  @param angle   旋转角度
 *
 *  @return
 */
CGAffineTransform  GetCGAffineTransformRotateAroundPoint(float centerX, float centerY,float x ,float y ,float angle)
{
    x = x - centerX; //计算(x,y)从(0,0)为原点的坐标系变换到(CenterX ，CenterY)为原点的坐标系下的坐标
    y = y - centerY; //(0，0)坐标系的右横轴、下竖轴是正轴,(CenterX,CenterY)坐标系的正轴也一样
    
    CGAffineTransform  trans = CGAffineTransformMakeTranslation(x, y);
    trans = CGAffineTransformRotate(trans,angle);
    trans = CGAffineTransformTranslate(trans,-x, -y);
    return trans;
}

/**
 *  画三角形箭头
 *
 *  @param angle 角度
 */
- (void)drawTriangleWithAngle:(CGFloat)angle{
    
    //设置三角形垂直居中
    CGRect r=self.arrowView.frame;
    r.origin.x=self.circleCenter.x-r.size.width/2.0;
    r.origin.y=self.circleCenter.y-(circleRadius+kArcLineWidth/2+5+r.size.height);
    self.arrowView.frame=r;

    //旋转到指定的角度
    if(angle!=-90){
        //angle+90 表示从垂直90向左右旋转,因为是以90度为中心点
        CGAffineTransform trans = GetCGAffineTransformRotateAroundPoint(self.circleCenter.x,r.origin.y+r.size.height/2,self.circleCenter.x,self.circleCenter.y,(angle+90)/180.0*M_PI);
        self.arrowView.transform = CGAffineTransformIdentity;
        self.arrowView.transform = trans;
    }
}

/**
 *  画扇形进度
 *
 *  @param lineColor  扇形颜色
 *  @param startAngle 开始角度
 *  @param endAngle   结束角度
 */
- (void)drawFannedLineColor:(UIColor *)lineColor startAngle:(CGFloat)startAngle endAngle:(CGFloat)endAngle{
    
    [lineColor set];  //设置线条颜色
    
    UIBezierPath *aPath = [UIBezierPath bezierPathWithArcCenter:self.circleCenter radius:circleRadius startAngle:degreesToRadians(startAngle) endAngle:degreesToRadians(endAngle) clockwise:YES];
    aPath.lineWidth = kArcLineWidth;
    aPath.lineCapStyle = kCGLineCapButt;  //线条拐角
    aPath.lineJoinStyle = kCGLineJoinRound;  //终点处理
    [aPath stroke]; //Draws line 根据坐标点连线

}


@end
